﻿package ahxu.crawler;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.utils.URIUtils;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.MicrosoftTagTypes;
import net.htmlparser.jericho.Segment;
import net.htmlparser.jericho.Source;

public class DefaultUriInputParser implements IUriInputParser {
	Log logger = LogFactory.getLog(DefaultUriInputParser.class);
	private List<String> fileWhileList = new ArrayList<String>();
	private List<String> fileBlackList = new ArrayList<String>();

	public DefaultUriInputParser() {

	}

	public Set<UriInput> doParser(CrawlerResult result) throws UriInputParserException {
		Set<UriInput> ret = new HashSet<UriInput>();
		String src = result.getStringSource();

		MicrosoftTagTypes.register();
		Source source=null;
		try {
			source = new Source(new ByteArrayInputStream(result.getSource()));
		} catch (IOException e1) {
			logger.error(e1.getMessage());
		}
		List<Element> elements = source.getAllElements(HTMLElementName.A);

		for (Element e : elements) {
			String href = e.getFirstStartTag().getAttributeValue("href");
			String text = e.getTextExtractor().toString();
			if(!StringUtils.isEmpty(StringUtils.trim(href)) && !StringUtils.isEmpty(StringUtils.trim(text))){
				try {
					UriInput input = new UriInput();
					URI inputUri=null;
					try{
						logger.info("get href from content:"+href);
						inputUri=URIUtils.resolve(result.getUri(), href);
					}catch(Exception ee){
						if(href.indexOf('?')!=-1){
							String href1=href.substring(0, href.indexOf('?'));
							String href2=href.substring(href.indexOf('?')+1);
							href=href1+"?"+URLEncoder.encode(href2,"UTF-8");
							inputUri=URIUtils.resolve(result.getUri(), href);
						}else{
							ee.printStackTrace();
						}
					}

						input.setUri(inputUri);
						input.setAnchorText(text);
						ret.add(input);
						logger.debug(href + "|" + text);

					
				} catch (Exception ex) {
					ex.printStackTrace();
					logger.warn(ex.getMessage());
					continue;
				}
			}else{
				continue;
			}
			
		}
		return ret;
	}

	public Set<UriInput> doParser2(CrawlerResult result) throws UriInputParserException {
		Set<UriInput> ret = new HashSet<UriInput>();
		String src = result.getStringSource();
		String tagAPatternStr = "<[aA]\\s*?([hH][rR][eR][fF]=[^>]+?)>(.*?)</[aA]>";
		Pattern tagAPattern = Pattern.compile(tagAPatternStr, Pattern.DOTALL);
		Matcher matcher = null;
		matcher = tagAPattern.matcher(src);

		while (matcher != null && matcher.find()) {
			String tagAStr = matcher.group();
			System.out.println(tagAStr);
			String startStr = null;
			String endStr = null;

			if (tagAStr.toLowerCase().contains("<a href=\"")) {
				startStr = "<a href=\"";
				endStr = "\"";
			} else if (tagAStr.toLowerCase().contains("<a href='")) {
				startStr = "<a href='";
				endStr = "'";
			} else if (tagAStr.toLowerCase().contains("<a href=")) {
				startStr = "<a href=";
				endStr = " ";
			} else {
				continue;
			}
			int hrefStartIndex = startStr.length();
			int hrefEndIndex = tagAStr.indexOf(endStr, hrefStartIndex);
			String href = tagAStr.substring(hrefStartIndex, hrefEndIndex);

			String temp = tagAStr.replaceAll("</[aA]>", "");
			int anchorStartIndex = temp.lastIndexOf('>') + 1;
			String anchor = temp.substring(anchorStartIndex);

			System.out.println(tagAStr + "|" + href + "|" + anchor);

		}
		return ret;
	}

}
